package com.jye.cloud.user.controller;



import com.jye.cloud.domain.JsonResult;
import com.jye.cloud.domain.User;
import com.jye.cloud.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags="用户控制器API")
public class UserController {

    @Autowired
    UserService userService;

    @Value("${publicvariable.WX_APP_ID}")
    private String WX_APP_ID;// appId

    @Value("${publicvariable.WX_APP_KEY}")
    private String WX_APP_KEY;// AppSecret

    @GetMapping("/test")
    public String test(){
        return "WX_APP_ID:"+WX_APP_ID+"\t "+ "WX_APP_KEY: "+WX_APP_KEY;
    }

    /*
    * 通过id查找用户
    * */
    @GetMapping("/findUserById")
    @ApiOperation(value="查找用户", notes="根据用户id查找用户", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "用户ID", required = true,
                    dataType = "Integer"),
    })
    public JsonResult findUserById(int id){
        JsonResult jsonResult = new JsonResult();
        User u = userService.findUserById(id);
        if (!StringUtils.isEmpty(u)){
            jsonResult.setData(u);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }


    @PutMapping("/updateUser")
    @ApiOperation(value="修改用户头像和昵称", notes="根据id,头像avatarurl,昵称nickname修改用户", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "user",
                    value = "用户", required = true),
    })
    public JsonResult updateUser(@RequestParam("user")User t_user){
        JsonResult jsonResult = new JsonResult();
        //避免修改其它
        User user = new User();
        user.setNickName(t_user.getNickName());
        user.setAvatarUrl(t_user.getAvatarUrl());
        user.setId(t_user.getId());

        int i = userService.updateUser(user);
        if(i!=0){
          jsonResult.setMsg("修改成功!");
        }else{
            jsonResult.setMsg("修改失败!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    @GetMapping("/findUserLoginHistoryById")
    @ApiOperation(value="查找用户登录历史记录", notes="根据id查找记录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "id",
                    value = "用户id", required = true),
    })
    public JsonResult findUserLoginHistoryById(int id){
        JsonResult jsonResult = new JsonResult();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Date> dateList = userService.findUserLoginHistoryById(id);
        List<String> list = new ArrayList<String>();
        String tmp;
        for (Date date : dateList) {
            tmp = formatter.format(date);
            list.add(tmp);
        }
        if(list.size()!=0){
            jsonResult.setData(list);
        }else{
            jsonResult.setCode("500");
            jsonResult.setMsg("抱歉!暂无您登录记录!如有异常请联系客服!");
        }
        return jsonResult;
    }


    @GetMapping("/login")
    @ApiOperation(value="登陆", notes="用户名和密码登录", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "username",
                    value = "用户名", required = true),
            @ApiImplicitParam(paramType="query",name = "password",
                    value = "密码", required = true),
    })
    public JsonResult login(User user){
        JsonResult jsonResult = new JsonResult();
        User t_user = userService.login(user.getUsername(),user.getPassword());
        if(!StringUtils.isEmpty(t_user)){
            jsonResult.setMsg("登录成功!");
            jsonResult.setData(t_user);
        }else{
            jsonResult.setMsg("用户名或者密码错误!请重试!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }

    @GetMapping("/isExitUsername")
    @ApiOperation(value="查看是否有重复用户名", notes="根据用户名查询是否有重复用户名", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "username",
                    value = "用户名", required = true),
    })
    public JsonResult isExitUsername(String username){
        JsonResult jsonResult = new JsonResult();
        Boolean flag = userService.isExitUsername(username);
        if(flag){
            jsonResult.setMsg("用户名可用!");
        }else{
            jsonResult.setMsg("用户名已存在!");
            jsonResult.setCode("500");
        }
        return jsonResult;
    }


    @PostMapping("/register")
    @ApiOperation(value="注册", notes="用户名username,密码password,头像avatarUrl,昵称nickName", httpMethod = "POST")
    public JsonResult register(@RequestBody User user){
        user.setAvatarUrl("http://39.106.100.179:9999/group1/M00/00/00/J2pks16oTJ2AClMsAAHw8xgOVfY843.jpg");
        System.out.println(user);
        int i = userService.addUser(user);
        if(i != 0){
            return  new JsonResult("200","注册成功!",null);
        }else{
            return  new JsonResult("500","注册失败!",null);
        }
    }







}
